<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
class order extends admin {
	protected $order_db, $order_detail_db;
	function __construct() {
		parent::__construct();
		$this->order_db = pc_base::load_model('order_model');
		$this->order_detail_db = pc_base::load_model('order_detail_model');
	}

	public function init() {
		$where = '1';
		if (isset($_GET['keywords']) && $_GET['keywords']!='') {
			if ($_GET['field']=='title' || $_GET['field']=='oid') {
				$where .= ' AND `'.$_GET['field'].'`="'.mysql_escape_string($_GET['keywords']).'"';
			} else {
				$user_db = pc_base::load_model('member_model');
				$user = $user_db->get_one(array('username'=>$_GET['keywords']), 'userid');
				if ($user) {
					$where .= ' AND `userid`="'.mysql_escape_string($user['userid']).'"';
				}
			}
			
		}
		
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$data = $this->order_db->listinfo($where,'id desc', $page, 20);
		$pages = $this->order_db->pages;
		
		$member_model = pc_base::load_model('member_model');
		foreach ($data AS &$d) {
			$memberinfo = $member_model->get_one(array('userid' => $d['userid']), 'username');
			$d['username'] = $memberinfo['username'];
		}
		
		include $this->admin_tpl('order_list');
 	}
 	
 	public function delete()
 	{
 		if($_GET['id']){
			if(is_array($_GET['id'])){
				$_GET['id'] = implode(',', $_GET['id']);
				$this->order_db->query("DELETE FROM `".$this->order_db->db_tablepre."order` WHERE `id` in ($_GET[id])");
			}else{
				$this->order_db->query("DELETE FROM `".$this->order_db->db_tablepre."order` WHERE `id` in ($_GET[id])");
			}
			showmessage('操作成功', '?m=order&c=order&a=init');
		}else{
			showmessage('参数不正确', '?m=order&c=order&a=init');
		}
 	}
 	
	public function listorder(){
		$ids = $_GET['id'];
		if($ids){
			foreach($ids as $n=>$id){
				if(!$id)continue;
				$this->order_db->update('`sort`='.intval($_GET['listorder'][$n]), array('id'=>$id));

			}
			showmessage('更新成功！', '?m=order&c=order&a=init');
		}else{
			showmessage('参数不正确', '?m=order&c=order&a=init');
		}
	}
	
	/**
	 * 清单详情列表
	 * 
	 */
	public function detail_list()
	{
		$order = $this->order_db->get_one(array('oid'=>$_GET['oid']));
		
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$data = $this->order_detail_db->listinfo(array('oid'=>$_GET['oid']),'id desc', $page, 20);
		$pages = $this->order_detail_db->pages;

		include $this->admin_tpl('order_detail_list');
	}
	
	/**
	 * 清单详情列表删除
	 */
	public function detail_delete()
 	{
 		if($_GET['id']){
			if(is_array($_GET['id'])){
				$_GET['id'] = implode(',', $_GET['id']);
				$this->order_detail_db->query("DELETE FROM `".$this->order_detail_db->db_tablepre."order_detail` WHERE `id` in ($_GET[id])");
			}else{
				$this->order_detail_db->query("DELETE FROM `".$this->order_detail_db->db_tablepre."order_detail` WHERE `id` in ($_GET[id])");
			}
			showmessage('操作成功', '?m=order&c=order&a=detail_list&oid='.$_GET['oid']);
		}else{
			showmessage('参数不正确', '?m=order&c=order&a=detail_list&oid='.$_GET['oid']);
		}
 	}
 	
 	/**
 	 * 更新缓存
 	 */
 	public function cache()
 	{
 		$num = 100;
 		if (!isset($_GET['total'])) {
 			$total = $this->order_detail_db->count();
 		} else {
 			$total = $_GET['total'];
 		}
 		if (!isset($_GET['limit'])) {
 			$limit = 0;
 		} else {
 			$limit = intval($_GET['limit']);
 		}
 		if ($limit > $total) {
 			showmessage('更新完成', APP_PATH.'index.php?m=brand&c=brand');
 		}
 		$data = $this->order_detail_db->select('', 'id,bid', $limit.','.$num, 'id DESC');
 		$brand_db = pc_base::load_model('brand_model');
 		foreach ($data AS $v) {
 			$update_data = array();
 			if ($v['bid'] > 0) {
 				$brand = $brand_db->get_one(array('id'=>$v['bid']), 'id,parentid,pcompanyname,prealname,pqq,paddress,ptel');
 				if ($brand) {
 					if ($brand['parentid']!=0) {
	 					$brand = $brand_db->get_one(array('id'=>$brand['parentid']), 'id,parentid,pcompanyname,prealname,pqq,paddress,ptel');
	 				}
	 				
	 				$update_data['companyname'] = $brand['pcompanyname'];
	 				$update_data['nickname'] = $brand['prealname'];
	 				$update_data['qq'] = $brand['pqq'];
	 				$update_data['tel'] = $brand['ptel'];
 				}
 			} else {
 				$update_data['companyname'] = '';
 				$update_data['nickname'] = '';
 				$update_data['qq'] = '';
 				$update_data['tel'] = '';
 			}
 			
 			if ($update_data) {
 				$this->order_detail_db->update($update_data, array('id'=>$v['id']));
 			}
 		}
 		if ($limit+$num > $total) {
 			showmessage('更新完成', APP_PATH.'index.php?m=brand&c=brand');
 		} else {
 			showmessage('已完成'.intval($limit+$num).'/'.$total.'!', APP_PATH.'index.php?m=order&c=order&a=cache&limit='.intval($limit+$num).'&total='.$total.'&menuid='.$_GET['menuid'].'&pc_hash='.$_GET['pc_hash']);
 		}
 	}
}
?>